<?php
/**
 *
 * @package    mahara
 * @subpackage artefact-feedback
 * @author     laurent.opprecht@gmail.com
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2011 University of Geneva http://www.unige.ch/
 *
 */

/**
 * A form to delete a feedback.
 */
class delete_parentfeedback_form {

    public static function create($id) {
        //Note: No _ in elements' name. This is used as a separator by the by pieform architecture
        $result = array(
            'name' => 'deleteparentfeeback'.$id,
            'renderer' => 'oneline',
            'successcallback' => 'deleteparentfeeback_submit',
            'elements' => array(
                'artefactid' => array('type' => 'hidden', 'value' => $id),
                'submit' => array(
                    'type' => 'submit',
                    'class' => 'mini_delete quiet',
                    'name' => 'delete_submit',
                    'value' => get_string('delete'),
                    'confirm' => get_string('confirmdelete', 'artefact.feedback'),
                ),
            ),
        );

        $result = pieform($result);
        return $result;
    }

    static function submit(Pieform $form, $values) {
        global $SESSION;

        $feedback = new ArtefactTypeFeedback((int) $values['artefactid']);

        $feedback->delete();
        $feedback->commit();

        $SESSION->add_ok_msg(get_string('viewarchived', 'artefact.version'));
        self::refresh();
    }

    static function refresh() {
        $url = stripos($_SERVER['SERVER_PROTOCOL'], 'HTTPS') !== false ? 'https://' : 'http://';
        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

        redirect($url);
    }
}

/**
 * Stub to inteface the pieform architecture
 *
 * @param Pieform $form
 * @param array $values
 */
function deleteparentfeeback_submit(Pieform $form, $values) {
    return delete_parentfeedback_form::submit($form, $values);
}

?>
